package com.farm.kaiping.controller;


import com.farm.kaiping.entity.SysDictItem;
import com.farm.kaiping.po.SysDictItemPO;
import com.farm.kaiping.service.SysDictItemService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.farm.common.util.PageParaUtils;
import com.farm.common.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 字典项(SysDictItem)表控制层
 *
 * @author zhuhui
 * @since 2024-11-26 22:58:22
 */
@RestController
@RequestMapping("/sysDictItem")
@AllArgsConstructor
@Api(tags = "字典项控制器")
public class SysDictItemController {

	private final SysDictItemService sysDictItemService;

	@ApiOperation(value = "分页查询字典项", notes = "权限：view_sysDictItem_page")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysDictItem_page')")
	@PostMapping("/page")
	public R<Page<SysDictItem>> getSysDictItemPage(@RequestBody SysDictItemPO sysDictItemPO) {
		try{
		Page page = PageParaUtils.withdrawPageByParamType(sysDictItemPO);
		return R.ok(sysDictItemService.getSysDictItemPage(page, sysDictItemPO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}
	
	@ApiOperation(value = "查询字典项列表", notes = "权限：view_sysDictItem_list")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysDictItem_list')")
	@PostMapping("/list")
	public R<List<SysDictItem>> getSysDictItemList(@RequestBody SysDictItemPO sysDictItemPO) {
		try{
			return R.ok(sysDictItemService.getSysDictItemList(sysDictItemPO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}

	@ApiOperation(value = "通过id查询字典项", notes = "权限 view_sysDictItem_view")
	@GetMapping("/{id}" )
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysDictItem_view')" )
	public R getById(@PathVariable("id" ) String id) {
		try{
			return R.ok(sysDictItemService.getById(id));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}
	
	@ApiOperation(value = "查询一条字典项", notes = "权限：view_sysDictItem_one")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysDictItem_one')")
	@PostMapping("/one")
	public R<SysDictItem> getSysDictItem(@RequestBody SysDictItemPO sysDictItemPO) {
		try{
			return R.ok(sysDictItemService.getSysDictItem(sysDictItemPO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}
	
	@ApiOperation(value = "新增字典项", notes = "权限：view_sysDictItem_add")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysDictItem_add')")
	@PostMapping("/add")
	public R saveSysDictItem(@RequestBody SysDictItemPO sysDictItemPO) {
		try{
		    return R.ok(sysDictItemService.saveSysDictItem(sysDictItemPO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}

	@ApiOperation(value = "修改字典项", notes = "权限：view_sysDictItem_edit")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysDictItem_edit')")
	@PostMapping("/edit")
	public R updateSysDictItem(@RequestBody SysDictItemPO sysDictItemPO) {
		try{
		    return R.ok(sysDictItemService.updateSysDictItem(sysDictItemPO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}    
	}

	@ApiOperation(value = "通过id删除字典项", notes = "权限：view_sysDictItem_del")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_sysDictItem_del')")
	@PostMapping("/del")
	public R removeSysDictItem(@RequestBody SysDictItemPO sysDictItemPO) {
		try{
		    return R.ok(sysDictItemService.removeSysDictItem(sysDictItemPO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}  
	}
	
}

